"""
auth:xgt-python
datetime:2021/11/18
爬取天气预报并做数据可视化
"""
# 导入相应的模块
import requests
from lxml import etree
import csv


def get_Weather(url):
    weather_info = []  # {'日期:....'最高气温':......}
    haeders = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36'
    }
    # 发送请求
    response = requests.get(url, headers=haeders)
    # 解析数据
    resp_html = etree.HTML(response.text)
    # xpath提取当页所有数据
    resp_list = resp_html.xpath("//ul[@class='thrui']/li")

    # for循环迭代遍历
    for li in resp_list:
        # 每天的数据放在字典中
        day_weather_info = {}
        # 日期  {'datetime':'2019-09-01'} 2019-09-01  星期日   2019-09-01
        day_weather_info['date_time'] = li.xpath('./div[1]/text()')[0].split(' ')[0]
        # 最高气温
        high = li.xpath("./div[2]/text()")[0]
        # 字符串的切割 索引
        day_weather_info['hight'] = high[:high.find('℃')]  # 28℃
        # 最低气温 字符串的切割方法
        low = li.xpath("./div[3]/text()")[0]
        day_weather_info['low'] = low[:low.find('℃')]  # 28℃
        # 天气状况
        day_weather_info['weather'] = li.xpath("./div[4]/text()")[0]
        weather_info.append(day_weather_info)

    # 返回数据
    # print(weather_info)
    return weather_info

# 全年的数据
weathers = []

# 确定爬取的数据来源
for month in range(1, 13):  # 拿到1 - 12的月份
    # 某年某月的天气信息
    # weather_time = '2019'+
    weather_time = '2019' + ('0' + str(month) if month < 10 else str(month))
    # if month < 10:
    #     weather_time = '2019'+('0',str(month))
    # else:
    #     weather_time = '2019'+str(month)
    url = f'https://lishi.tianqi.com/changsha/{weather_time}.html'
# 调用函数
    weather = get_Weather(url)
 # 每月数据存入年数据
    weathers.append(weather)
print(weathers)


# 数据一次写入 csv
with open('weather.csv',mode='w',encoding='utf-8',newline='') as csvfile:
    writer = csv.writer(csvfile)

    # 写入列名:colums_name
    writer.writerow(['日期','最高气温','最低气温','天气'])
     # 一次写入这么对行用writerows(写入的数据是列表)
    # 列表推导式
    writer.writerows([list(day_weather_dict.values()) for month_weather in weathers for day_weather_dict in month_weather])

    list_year = []
    for month_weather in weathers:
        for day_weather_dict in month_weather:
            list_year.append(list(day_weather_dict.values()))
    writer.writerows(list_year)




